﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace VietNamKey
{
    [DefaultEvent("SettingChanged")]
    public partial class ucAdvanced : UserControl
    {
        public event EventHandler SettingChanged;
        bool isOk = false;
        public ucAdvanced()
        {
            InitializeComponent();
            //if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
                Reload();
        }

        public void Reload()
        { 
            isOk = false;
            chbKTChinhTa.Checked = Global.KeySettings.UseCheckSpelling;
            chbRestoreEN.Checked = Global.KeySettings.UseRestoreEN;
			cbbCheckSpellingLevel.SelectedIndex = Global.KeySettings.CheckSpellingLevel;
            
            chbBoDauKieuMoi.Checked = Global.KeySettings.UseNewStyle;
            chbSuDungSendInput.Checked = Global.KeySettings.UseSendKey;
            chbBoDauTrongAutoComplete.Checked = Global.KeySettings.UseVnInAutoComplete;

            chbChoPhepGoTat.Checked = Global.KeySettings.UseShorthand;
            chbGoTatKhiTatTiengViet.Checked = Global.KeySettings.UseShorthandInEn;

            chbKhoiDongCungWin.Checked = Global.KeySettings.RunAtWindowsStart;
            chbBatHoiThoaiKhiKhoiDong.Checked = Global.KeySettings.OpenDialogWhenStart;

            chbIC_F2PH.Checked = Global.KeySettings.IC_F2PH;
			chbIC_D2DD_Z2D.Checked = Global.KeySettings.IC_D2DD_Z2D;
			chbIC_J2GI.Checked = Global.KeySettings.IC_J2GI;
			chbIC_Q2QU.Checked = Global.KeySettings.IC_Q2QU;
			chbIC_K2KH_C2K.Checked = Global.KeySettings.IC_K2KH_C2K;
			chbIC_G2GH.Checked = Global.KeySettings.IC_G2GH;
			chbIC_NG2NGH.Checked = Global.KeySettings.IC_NG2NGH;
			chbFC_G2NG.Checked = Global.KeySettings.FC_G2NG;
			chbFC_H2NH.Checked = Global.KeySettings.FC_H2NH;
			chbFC_K2CH.Checked = Global.KeySettings.FC_K2CH;
			chbAutoAcute.Checked = Global.KeySettings.AutoAcute;
			grbIC.Enabled = chbAutoIC.Checked = Global.KeySettings.AutoIC;
			grbFC.Enabled = chbAutoFC.Checked = Global.KeySettings.AutoFC;
			
            cbbVowelsShorthand.SelectedIndex = Global.KeySettings.VowelsShorthand;
            isOk = true;
        }
        private void RaiseEvent(object sender, EventArgs e)
        { 
            if (!isOk) return;
            Global.KeySettings.UseCheckSpelling = chbKTChinhTa.Checked;
			Global.KeySettings.UseRestoreEN = chbRestoreEN.Checked;
			Global.KeySettings.CheckSpellingLevel = cbbCheckSpellingLevel.SelectedIndex;
				
            Global.KeySettings.UseNewStyle = chbBoDauKieuMoi.Checked;
            Global.KeySettings.UseSendKey = chbSuDungSendInput.Checked;
            Global.KeySettings.UseVnInAutoComplete = chbBoDauTrongAutoComplete.Checked;

            Global.KeySettings.UseShorthand = chbChoPhepGoTat.Checked;
            Global.KeySettings.UseShorthandInEn = chbGoTatKhiTatTiengViet.Checked;

            Global.KeySettings.RunAtWindowsStart = chbKhoiDongCungWin.Checked;
            Global.KeySettings.OpenDialogWhenStart = chbBatHoiThoaiKhiKhoiDong.Checked;
			
            Global.KeySettings.IC_F2PH = chbIC_F2PH.Checked; 	
			Global.KeySettings.IC_D2DD_Z2D = chbIC_D2DD_Z2D.Checked;
			Global.KeySettings.IC_J2GI = chbIC_J2GI.Checked; 	
			Global.KeySettings.IC_Q2QU = chbIC_Q2QU.Checked; 	
			Global.KeySettings.IC_K2KH_C2K = chbIC_K2KH_C2K.Checked;
			Global.KeySettings.IC_G2GH = chbIC_G2GH.Checked; 	
			Global.KeySettings.IC_NG2NGH = chbIC_NG2NGH.Checked;
			Global.KeySettings.FC_G2NG = chbFC_G2NG.Checked;
			Global.KeySettings.FC_H2NH = chbFC_H2NH.Checked;
			Global.KeySettings.FC_K2CH = chbFC_K2CH.Checked;
            Global.KeySettings.AutoAcute = chbAutoAcute.Checked;
            Global.KeySettings.AutoIC = chbAutoIC.Checked;
            Global.KeySettings.AutoFC = chbAutoFC.Checked;
            
            Global.KeySettings.VowelsShorthand = cbbVowelsShorthand.SelectedIndex;
            if (SettingChanged != null) SettingChanged(this, new EventArgs());
        }

        private void btnDefaultSetting_Click(object sender, EventArgs e)
        {
            Global.LoadDefaultSetting();
            Reload();
            if (SettingChanged != null) SettingChanged(this, new EventArgs());
        }

        private void btnLoadSetting_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "XML|*.xml";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Global.KeySettings = ObjectXMLSerializer<KeySettings>.Load(dialog.FileName);
                    Reload();
                    if (SettingChanged != null) SettingChanged(this, new EventArgs());
                }
                catch
                { 
                    MessageBox.Show("Không đọc được cấu hình!"); 
                }
            }
        }

        private void btnSaveSetting_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "XML|*.xml";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    ObjectXMLSerializer<KeySettings>.Save(Global.KeySettings, dialog.FileName);
                }
                catch
                {
                    MessageBox.Show("Không ghi được cấu hình!");
                }
            }
        }
        
        void ChbAutoICCheckedChanged(object sender, EventArgs e)
        {
        	if(!chbAutoIC.Checked)
        	{
        		chbIC_F2PH.Checked = 
				chbIC_D2DD_Z2D.Checked = 
				chbIC_J2GI.Checked = 
				chbIC_Q2QU.Checked = 
				chbIC_K2KH_C2K.Checked = 
				chbIC_G2GH.Checked = 
				chbIC_NG2NGH.Checked = false;
        	}
        	grbIC.Enabled = chbAutoIC.Checked;
        	RaiseEvent(sender, e);
        }
        
        void ChbAutoFCCheckedChanged(object sender, EventArgs e)
        {
        	if(!chbAutoFC.Checked)
        	{
        		chbFC_G2NG.Checked =
				chbFC_H2NH.Checked =
				chbFC_K2CH.Checked = false;
        	}
        	grbFC.Enabled = chbAutoFC.Checked;
        	RaiseEvent(sender, e);
        }
    }
}
